/*
 * @ created for matlabMe by Leszek Piątek, 2009
 */

package main;

import com.sun.lwuit.plaf.Style;
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import com.sun.lwuit.Container.*;
import com.sun.lwuit.Component.*;
import com.sun.lwuit.layouts.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.SelectionListener;
import java.io.*;
import interpreter.Parser;
import variables.Variables;

public class MatlabMe extends MIDlet implements ActionListener {
    private static final String version = "0.2";
    private static final int EXIT_COMMAND = 1;
    private static final Command exitCommand = new Command("Exit", EXIT_COMMAND);
    private static myTextArea cmdLineOutput = null;
    private static myTextArea variableOutput = null;
    private static Variables variables = null;
    private static myTextArea cmdLine = null;
    private static TabbedPane tp = null;

    private class myTextArea extends TextArea {
        private int history=5; //how many lines we see on TextArea
        public myTextArea (int row, int col, int constraint) {
            super(row, col, constraint);
        }
        public void addText (String s) {
            int sLines=0;
            for(int i=history;i<s.length();i++) if(s.substring(i, i+1).compareTo("\n")==1) sLines++;
            if(this.getLines()+sLines>history) {
                String tmpStr = "";
                for(int i=sLines;i<this.getLines();i++) {
                    tmpStr += this.getTextAt(i)+"\n";
                }
                this.setText(tmpStr+s);
            } else {
                this.setText(this.getText()+s);
            }
        }
    }

    private class TabbedPaneListener implements SelectionListener {
        public void selectionChanged(int arg0, int arg1) {
             if(arg1==2) {
                 String s = "";
                 for (int i=0;i<variables.size();i++) {
                    s+=variables.getId(i)+" = "+variables.get(i).toString();
                 }
                 variableOutput.setText(s);
             }
        }
    }

    private class BtnListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            Button btn = (Button)e.getSource();
            String desc = btn.getText();
            if(desc.equals("ans")) {
                Parser yyparser = new Parser(new InputStreamReader( new ByteArrayInputStream( (cmdLine.getText()+"\n").getBytes())));
                yyparser.parse();

               //get result from parser and print it on cmdLineOutput
                cmdLineOutput.addText(">> "+cmdLine.getText()+"\n"+yyparser.getOutput());

               //clear the cmdLine
                cmdLine.setText("");
               //move to tab nr 1 - output
                tp.setSelectedIndex(1);
            } else if (desc.equals("<- backspace") || desc.equals("<-")) {
                if(cmdLine.getText().length()>0) {
                    cmdLine.setText(cmdLine.getText().substring(0, cmdLine.getText().length()-1));
                }
            }
            else {
                cmdLine.setText(cmdLine.getText()+desc);
            }
        }
    }

    private class Btn extends Button {
            public Btn(String desc) {
                super(desc);
                this.setAlignment(Label.CENTER);
                this.addActionListener(new BtnListener());
            }
    }

    public void startApp() {
        Display.init(this);
        try {
            Resources r = Resources.open("/businessTheme.res");
            UIManager.getInstance().setThemeProps(r.getTheme("businessTheme"));
        } catch (IOException ioe) {
            System.out.println("Couldn't load theme.");
        }
        
        Form f = new Form("matlabMe "+version);
        f.setLayout(new BorderLayout());
        tp=new TabbedPane();
        tp.addTabsListener(new TabbedPaneListener());

     //tab1
        Container tab1Container = new Container(new BoxLayout(BoxLayout.Y_AXIS));
       //command line
        cmdLine = new myTextArea(1, 80, TextArea.ANY);
        cmdLine.setEditable(false);
        Label cmdLineLabel = new Label("Cmd line:");
        tab1Container.addComponent(cmdLineLabel);
        tab1Container.addComponent(cmdLine);
       //buttonsPanel
        Container buttonsPanel = new Container(new GridLayout(5,7));
        Button b29 = new Btn(".");
        buttonsPanel.addComponent(b29);
        Button b30 = new Btn(",");
        buttonsPanel.addComponent(b30);
        Button b31 = new Btn("'");
        buttonsPanel.addComponent(b31);
        Button b32 = new Btn("A");
        buttonsPanel.addComponent(b32);
        Button b33 = new Btn("B");
        buttonsPanel.addComponent(b33);
        Button b34 = new Btn("<-");
        buttonsPanel.addComponent(b34);
        Button b35 = new Btn("ans");
        Style style=new Style(b35.getStyle());
        style.setBgColor(0xa90000);
        b35.setStyle(style);
        buttonsPanel.addComponent(b35);
        //first
        Button b1 = new Btn("1");
        buttonsPanel.addComponent(b1);
        Button b2 = new Btn("2");
        buttonsPanel.addComponent(b2);
        Button b3 = new Btn("3");
        buttonsPanel.addComponent(b3);
        Button b4 = new Btn("[");
        buttonsPanel.addComponent(b4);
        Button b5 = new Btn("]");
        buttonsPanel.addComponent(b5);
        Button b6 = new Btn(";");
        buttonsPanel.addComponent(b6);
        Button b7 = new Btn("=");
        //second row
        buttonsPanel.addComponent(b7);
        Button b8 = new Btn("4");
        buttonsPanel.addComponent(b8);
        Button b9 = new Btn("5");
        buttonsPanel.addComponent(b9);
        Button b10 = new Btn("6");
        buttonsPanel.addComponent(b10);
        Button b11 = new Btn("(");
        buttonsPanel.addComponent(b11);
        Button b12 = new Btn(")");
        buttonsPanel.addComponent(b12);
        Button b13 = new Btn("|");
        buttonsPanel.addComponent(b13);
        Button b14 = new Btn("^");
        buttonsPanel.addComponent(b14);
        //third row
        Button b15 = new Btn("7");
        buttonsPanel.addComponent(b15);
        Button b16 = new Btn("8");
        buttonsPanel.addComponent(b16);
        Button b17 = new Btn("9");
        buttonsPanel.addComponent(b17);
        Button b18 = new Btn("+");
        buttonsPanel.addComponent(b18);
        Button b19 = new Btn("-");
        buttonsPanel.addComponent(b19);
        Button b20 = new Btn("*");
        buttonsPanel.addComponent(b20);
        Button b21 = new Btn("/");
        buttonsPanel.addComponent(b21);
        //fourth row
        Button b22 = new Btn("0");
        buttonsPanel.addComponent(b22);
        Button b23 = new Btn(":");
        buttonsPanel.addComponent(b23);
        Button b24 = new Btn(".^");
        buttonsPanel.addComponent(b24);
        Button b25 = new Btn(".+");
        buttonsPanel.addComponent(b25);
        Button b26 = new Btn(".-");
        buttonsPanel.addComponent(b26);
        Button b27 = new Btn(".*");
        buttonsPanel.addComponent(b27);
        Button b28 = new Btn("./");
        buttonsPanel.addComponent(b28);
        tab1Container.addComponent(buttonsPanel);
      //end buttonsPanel
        tp.addTab("cmd line", tab1Container);
      //end tab 1
      
      //tab2
        variables = new Variables();
        Container tab2Container = new Container(new FlowLayout());
      //command line output TextArea
        cmdLineOutput = new myTextArea(12, f.getWidth()/7, TextArea.ANY);
        cmdLineOutput.setEditable(false);
        cmdLineOutput.setFocus(false);
        cmdLineOutput.setText(" >> Hello! \n What can I count for you? \n");
        cmdLineOutput.setLinesToScroll(10);
        cmdLineOutput.setMaxSize(12);
        
        tab2Container.addComponent(new Label("Cmd line output:"));
        tab2Container.addComponent(cmdLineOutput);
        tab2Container.addComponent(new Label(""));
        tp.addTab("output", tab2Container);
      //end tab2
        
      //tab3
        variables = new Variables();
        Container tab3Container = new Container(new FlowLayout());
      //variables browser
        variableOutput = new myTextArea(12, f.getWidth()/7, TextArea.ANY);
        variableOutput.setEditable(false);
        variableOutput.setText("");
        variableOutput.setLinesToScroll(10);

        tab3Container.addComponent(new Label("Defined variables:"));
        tab3Container.addComponent(variableOutput);
        tab3Container.addComponent(new Label(""));
        
        tp.addTab("vars", tab3Container);
       //end tab3

       //tab4
        Container tab4Container = new Container(new FlowLayout());
        myTextArea aboutText = new myTextArea(10, f.getWidth()/7, TextArea.ANY);
        aboutText.setEditable(false);
        aboutText.setText("matlabMe was created as an academic project for 'Formal languages and Compilers' lecture at AGH Univeristy of Science and Technology in Kraków, Poland.\n\nAny feedback highly appreciated! code.google.com/p/matlabme or lpiatek at gmail.com");
        tab4Container.addComponent(new Label("About:"));
        tab4Container.addComponent(aboutText);
        tp.addTab("about", tab4Container);
       //end tab4

       //add TabbedPane to form
        f.addComponent(BorderLayout.CENTER, tp);
        
        //define menu
        f.addCommand(exitCommand);
        f.setCommandListener(this);

        f.show();
    }

    public void actionPerformed(ActionEvent evt) {
        Command cmd = evt.getCommand();
        switch (cmd.getId()) {
            case EXIT_COMMAND:
                notifyDestroyed();
                break;
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    
}
